﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServiceCheckOleAutomationEnabled : IssueTypeCheckBase
    {

        public ServiceCheckOleAutomationEnabled()
        {
            IssueTypeId = 2005;
        }

        public override void Check()
        {
            try
            {
                // Service check Ole Automation is enabled
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


                /*
                 * This rule checks if OLE procedures is enabled 
                 */

                var Qry = from ss in dc.ServiceSettings

                          where (ss.Name == "Ole Automation Procedures") && (Convert.ToInt32(ss.Value) == 1)


                          select ss;

                foreach (ServiceSetting serviceSetting in Qry)
                {
                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                    //Create new Issue

                    Issue issue = new Issue();
                    issue.IssueType = issueType;
                    issue.TargetId = serviceSetting.ServiceId;

                    string serviceName = serviceSetting.Service.InstanceName;

                    issue.Description = issueType.Description.Replace("{InstanceName}", serviceSetting.Service.InstanceName);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }

    }
}
